<template>
  <div>
    <Header
      :backQuery="{
        checkedTypeId: this.checkedTypeId,
        checkedVideoType: this.checkedVideoType,
        checkedVideoTypeName: this.checkedVideoTypeName,
      }"
    ></Header>
    <div class="read-test-list test-center w1200 grammar-test-list">
      <div class="test-container table-data">
        <div class="test-container-header flex-space-be">
          <div class="class-name">
            <span>
              当前课程：
              {{
                checkedBookData.edition_name + "-" + checkedBookData.grade_name
              }}
            </span>
          </div>
        </div>
        <div class="info-table">
          <div class="unit-select common-select pt20 mb20">
            <span>选择分类: </span>
            <el-select
              v-model="checkedTypeId"
              @change="checkTypeId"
              placeholder="请选择"
              class="mr10"
            >
              <el-option label="全部" :value="null"> </el-option>
              <el-option
                v-for="categoryData in videoListData"
                :key="categoryData.id"
                :label="categoryData.title"
                :value="categoryData.id"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="checkedVideoType"
              @change="checkVideoType"
              placeholder="请选择"
            >
              <el-option label="全部" :value="null"> </el-option>
              <el-option
                v-for="classiflyData in checkedVideoList"
                :key="classiflyData.id"
                :label="classiflyData.title"
                :value="classiflyData.id"
              >
              </el-option>
            </el-select>
          </div>
          <div class="clear-float common-table common-pagination">
            <el-table border :data="paperData.list" style="width: 100%">
              <el-table-column prop="english_grammar_id" label="类型">
                <template slot-scope="scope">
                  <div>
                    {{ getGrammarName(scope.row.english_grammar_id) }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="english_grammar_id" label="分类">
                <template slot-scope="scope">
                  <div>
                    {{ getGrammarTypeName(scope.row.english_grammar_id) }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="question_num" label="题目数量">
              </el-table-column>
              <el-table-column prop="use_time" label="做题速度(秒)">
              </el-table-column>
              <el-table-column prop="last_correct_rate" label="正确率">
                <template slot-scope="scope">
                  <div class="table-text">
                    {{ scope.row.last_correct_rate }}%
                  </div>
                </template>
              </el-table-column>

              <el-table-column prop="finish_time" label="时间" width="180">
              </el-table-column>
              <el-table-column prop="address" label="详细">
                <template slot-scope="scope">
                  <div
                    class="table-text click"
                    @click="openTestPaper(scope.row.test_id)"
                  >
                    查看
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <div class="pt20">
              <el-pagination
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
                class="fr"
                background
                :page-size="paperParams.pageSize"
                layout="prev, pager, next"
                :total="paperData.total_num"
                hide-on-single-page
                :current-page.sync="paperParams.pageNum"
                v-if="showPagination"
                :page-sizes="[2, 5, 10, 15, 20, 25, 30]"
              >
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      title="查看详情"
      :visible.sync="dialogTestDataVisible"
      class="test-data-info"
    >
      <QuestionList :paperData="testData"></QuestionList>
    </el-dialog>
  </div>
</template>
<script>
import Header from "@/components/Header";
import QuestionList from "../components/grammarQuestionList";
import { getGrammarPaperById, getGrammarPaperList } from "@/api/index";
export default {
  data() {
    return {
      checkedBookData: null,
      paperParams: {
        pageSize: 10,
        pageNum: 1,
      },
      paperData: {
        total_num: 0,
        list: [],
      },
      showPagination: true,
      dialogTestDataVisible: false,
      testData: {},
      checkedQuestionType: null,
      videoListData: [],
      checkedTypeId: null,
      checkedVideoType: null,
    };
  },
  components: {
    Header,
    QuestionList,
  },
  computed: {
    checkedVideoList() {
      let data1 = [];
      data1 = this.videoListData.filter((item) => {
        return item.id == this.checkedTypeId;
      });
      if (data1.length > 0) {
        return data1[0].english_grammar_vos;
      }
      return [];
    },
  },
  methods: {
    getGrammarName(grammarTypeId) {
      let title = "";
      this.videoListData.forEach((grammar) => {
        grammar.english_grammar_vos.forEach((grammarType) => {
          if (grammarType.id == grammarTypeId) {
            title = grammar.title;
          }
        });
      });
      return title;
    },
    getGrammarTypeName(grammarTypeId) {
      let title = "";
      this.videoListData.forEach((grammar) => {
        grammar.english_grammar_vos.forEach((grammarType) => {
          if (grammarType.id == grammarTypeId) {
            title = grammarType.title;
          }
        });
      });
      return title;
    },
    openTestPaper(id) {
      getGrammarPaperById(id)
        .then((res) => {
          if (res.code == 200) {
            this.testData = {
              isAnswer: true,
              score: res.data.score,
              questionList: res.data.student_grammar_question_answer_info_list.map(
                (question) => {
                  return {
                    questionId: question.question_id,
                    title: question.title,
                    inputNum: question.inputNum,
                    answerType: question.answer_type,
                    answerList: question.article_answer_info_list,
                    isTrue:
                      question.student_article_answer_info_list.length > 0 &&
                      question.student_article_answer_info_list.every(
                        (answer) => {
                          return answer.is_true == 1;
                        }
                      ),
                    parse: question.parse,
                    trueAnswer: question.article_answer_info_list.filter(
                      (answer) => {
                        return answer.is_true == 1;
                      }
                    ),
                    studentAnswer: question.student_article_answer_info_list
                      .filter((answer) => {
                        return answer.answer_id || answer.answer;
                      })
                      .map((answer) => {
                        return answer.answer_id || answer.answer;
                      }),
                    verifyAnswer: question.student_article_answer_info_list,
                  };
                }
              ),
            };
            this.dialogTestDataVisible = true;
          }
        })
        .catch(() => {});
    },
    checkTypeId() {
      this.checkedVideoType = null;
      this.changeGetWord();
    },
    checkVideoType() {
      this.changeGetWord();
    },
    changeGetWord() {
      this.showPagination = false;
      this.paperParams.pageNum = 1;
      this.$nextTick(() => {
        this.showPagination = true;
      });
      this.getGrammarPaperTest();
    },
    handleCurrentChange(value) {
      this.paperParams.pageNum = value;
      this.getGrammarPaperTest();
    },
    handleSizeChange(value) {
      this.paperParams.pageSize = value;
      this.changeGetWord();
    },
    getGrammarPaperTest() {
      let grade = null;
      console.log(this.checkedBookData);
      if (this.checkedBookData.subject_id == 102) {
        grade = 998;
      }
      if (this.checkedBookData.subject_id == 209) {
        grade = 999;
      }
      if(this.checkedBookData.book_id==2070){
        grade = 998;
      }
      if(this.checkedBookData.book_id==2071){
        grade = 999;
      }
      let checkedTypeIdList = null;
      if (this.checkedTypeId) {
        if (this.checkedVideoType) {
          checkedTypeIdList = [this.checkedVideoType];
        } else {
          checkedTypeIdList = this.checkedVideoList.map((item) => {
            return item.id;
          });
        }
      }
      let data = {
        page_require: {
          page_num: this.paperParams.pageNum,
          page_size: this.paperParams.pageSize,
        },
        book_id: this.checkedBookData.book_id,
        grade: grade,
        english_grammar_id_list: checkedTypeIdList,
      };
      getGrammarPaperList(data).then((res) => {
        this.paperData = res.data;
      });
    },
  },
  created() {
    this.checkedBookData = JSON.parse(localStorage.getItem("bookData"));
    this.videoListData = JSON.parse(localStorage.getItem("videoListData"));
    if (!this.videoListData) {
      this.$router.push({
        name: "grammarVideo",
      });
    }
    this.checkedTypeId = this.$route.query.checkedTypeId
      ? Number(this.$route.query.checkedTypeId)
      : null;
    this.checkedVideoType =
      this.$route.query.checkedTypeId && this.$route.query.checkedVideoType
        ? Number(this.$route.query.checkedVideoType)
        : null;
    this.checkedVideoTypeName = this.$route.query.checkedVideoTypeName;
    this.getGrammarPaperTest();
  },
  props: {
    type: {
      type: Number,
    },
    unitList: {
      type: Array,
      default: function () {
        return [];
      },
    },
  },
};
</script>